package com.kk.single;

import java.util.HashSet;

/**
 * 懒汉式
 * 线程不安全
 * 多跑几次就出现了不同的实例
 *
 * @author 王志强
 * @date 2021-12-09
 */
public class Singleton1 {
    private static Singleton1 instance;

    private Singleton1() {
    }

    public static Singleton1 getInstance() {
        if (instance == null) {
            instance = new Singleton1();
        }
        return instance;
    }


    public static void main(String[] args) {
        HashSet<Object> hashSet = new HashSet<>();
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                Singleton1 instance = Singleton1.getInstance();
                hashSet.add(instance);
            }).start();
        }

        System.out.println(hashSet);
    }
}
